// pages/result/index.js
var api = require('../../request/api.js');
var util = require('../../utils/util');
//import * as echarts from "../../utils/ec-canvas/echarts.min"

var tizhiTableXData = [] // '1223-01','1223-02','1223-03','1223-04'
var tizhiTableYData = []
var chartOption = {
    title: {
        show: true,
        left: 'center',
        text: '体质变化趋势图',
        textStyle: {
            color: '#461b6e',
            fontWeight: 'normal'
        }
    },
    // legend: {
    //   data: ['气虚体质'],
    //   top: 20,
    //   left: 10,
    //   z: 100
    // },
    grid: {
        left: 40,//折线图距离左边距
        right: 20,//折线图距离右边距
        top: 50,//折线图距离上边距
        bottom: 25,
        containLabel: false
    },
    tooltip: {
        show: false,
        trigger: 'axis'
    },
    xAxis: {
        type: 'category',
        data: tizhiTableXData
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: function (value) {
                var texts = [];
                if (value == 1) {
                    texts.push('平和');
                }
                else if (value == 2) {
                    texts.push('气虚');
                }
                else if (value == 3) {
                    texts.push('血虚');
                }
                else if (value == 4) {
                    texts.push('阴虚');
                }
                else if (value == 5) {
                    texts.push('淤血');
                }
                else if (value == 6) {
                    texts.push('气郁');
                }
                else if (value == 7) {
                    texts.push('湿热');
                } else if (value == 8) {
                    texts.push('痰湿');
                } else if (value == 9) {
                    texts.push('阳虚');
                }
                return texts;
            }
        },
    },
    series: [
        {
            data: tizhiTableYData,
            type: 'line',
            smooth: true,
            lineStyle: {
                color: '#b047c6',
                width: 2,
            },
            itemStyle: {
                borderWidth: 4,
                color: '#b047c6'
            }
        }
    ]
}

const App = getApp()

Page({
    /**
     * 页面的初始数据
     */
    data: {
      barHeight:App.globalData.paddingTop,

        styledata: {
            "bg_color": "white",
            "color": "#000",
            "leftflag": true,
            "homeflag": true,
            "name": "诊测结果"
        },
        ec: {
            // lazyLoad: true, //初始化加载
            //onInit: initChart
        },
        tongue_path_photo: '',
        result: {
            state: -1,
            showBackhomeBtn: false,
            typeName: "",
            host: "",
            tonguePicAddr: "logo.png",
            recomand: "",

            score: 0,
            color: "#33cd5f",  //#ef473a
            id: 0
        },
        file_host: api.FILE_HOST,
        type: {
            "气虚": "气虚质",
            "正常": "平和质",
            "阴虚": "阴虚质",
            "阳虚": "阳虚质",
            "气郁": "气郁质",
            "湿热": "湿热质",
            "痰湿": "痰湿质",
            "淤血": "血瘀质",
            "血虚": "血虚质"
        },
        kg: [],
        firstItem: {},
        male: null
    },
    // onUnload:function (options) {
    // },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.setData({
        //   state: wx.getStorageSync('state'),
        //   file_host: api.FILE_HOST
        // })
        let id = options.id
        let from = options.from
        if (from) {
            this.setData({
                from: from
            })
        }
        if (id) {
            this.setData({
                id: id
            })
            let that = this
            wx.getStorage({
                key: 'tongue_path',
                success(res) {
                    that.setData({
                        tongue_path_photo: res.data
                    })
                }
            })
            wx.getStorage({
                key: 'result',
                success(res) {
                    that.setData({
                        result: res.data,
                    })
                }
            })
            //this.getData(id)
        } else {

            let tizhiTableData = JSON.parse(options.tizhiTableData)
            console.log(tizhiTableData)
            chartOption.xAxis.data = tizhiTableData.tizhi_tableXData || []
            chartOption.series[0].data = tizhiTableData.tizhi_tableYData || []
            let that = this
            //   wx.getStorage({
            //     key: 'result',
            //     success(res) {
            //       that.setData({
            //         result: res.data,
            //         id: res.data.id,
            //         showBackhomeBtn: true
            //       })
            //       // that.setColor(res.data.score)

            //     }
            //   })
        }
        // let shared = parseInt(wx.getStorageSync('shared'))
        // if (!shared){
        //   wx.getStorage({
        //     key: 'count',
        //     success: function (res) {

        //       if (res.data > 2) {
        //         // 提示分享
        //         $wuxDialog().open({
        //           resetOnClose: true,
        //           title: '提示',
        //           content: '分享可以获得20个健康分。',
        //           buttons: [{
        //             text: '取消',
        //           },
        //           {
        //             text: '好的',
        //             type: 'primary',
        //             openType: 'share'
        //           },
        //           ],
        //         })
        //       }

        //     },
        //   })
        // }
    },

    getData: function (id) {

        // let that = this
        // util.request(api.API_GET_HISTORY_BYID, { id: id }, function (res) {
        //   // let pos = res.data.recomand.indexOf("保养方法：")
        //   // let length = res.data.recomand.length
        //   // res.data.recomand2 = res.data.recomand.substring(pos+5, length)
        //   // res.data.recomand = res.data.recomand.substring(0, pos)
        //   chartOption.xAxis.data = res.data.tizhiTableXData
        //   chartOption.series[0].data = res.data.tizhiTableYData
        //   that.setData({
        //     result: res.data,
        //   })
        // });

    },

    preview: function () {
        wx.previewImage({
            current: api.FILE_HOST + this.data.result.tonguePicAddr, // 当前显示图片的http链接
            urls: [api.FILE_HOST + this.data.result.tonguePicAddr] // 需要预览的图片http链接列表
        })
    },
    goHome: function () {
        wx.navigateBack({
            delta: 0,
        })
    },
    supportMe() {
        wx.previewImage({
            urls: ['https://www.aikanshe.com/img/zanCode.jpg'],
            current: 'https://www.aikanshe.com/img/zanCode.jpg' // 当前显示图片的http链接      
        })
        // 记录点赞
        let that = this
        util.request(api.API_SUPPORT_ME, { id: that.data.id }, function (res) {

        });
    },
    onShareAppMessage(res) {
        let id = this.data.id
        let type = this.data.result.typeName
        if (res.from === 'button') {
            // 来自页面内转发按钮
            wx.setStorage({
                key: 'shared',
                data: 1,
            })
            wx.getStorage({
                key: 'count',
                success: function (res) {
                    wx.setStorage({
                        key: 'count',
                        data: res.data - 1
                    })

                },
            })
        }

        return {
            title: '全网首款不花钱的掌上CT，很准，你也来试试？',
            path: 'pages/tongue/detail?id=' + id
        }
    },
    onShareTimeline: function (res) {
        return {
            title: '全网首款不花钱的掌上CT，很准，你也来试试？',
            query: this.data.id
        }
    },
    onPageBack() {
        // 收集意见
        let that = this
        if (that.data.from != 'temp') {
            return wx.navigateBack({
                delta: 1
            })
        }
        wx.showModal({
            title: '稍等一下',
            content: '本次检测结果您满意吗？',
            cancelText: "不满意", //默认是“取消”
            cancelColor: 'gray', //取消文字的颜色
            confirmText: "满意", //默认是“确定”
            confirmColor: 'purple', //确定文字的颜色
            success: function (res) {
                const ok = res.confirm ? 1 : 0;
                util.request(api.API_SUPPORT_ME, { id: that.data.id, ok: ok }, function (res) {
                    wx.navigateBack({
                        delta: 1
                    })
                })
            }
        })
    }
})